package ssi.actions;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import ssi.beans.EditUserForm;
import ssi.tools.Connector;
import ssi.tools.Tool;

public class EditUserAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		request.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession(true);
		EditUserForm user=(EditUserForm)form;
		int id =Integer.parseInt(user.getId());
		String name=user.getName();
		String surname=user.getSurname();
		String email=user.getEmail();
		String address=user.getAddress();
		String pesel=user.getPesel();
		ActionErrors actionErrors = new ActionErrors();
		String target=null;
		
		try{
			Connection connection = new Connector().getConnection();
    		Statement statement = connection.createStatement();
    		statement.setMaxRows(1);
    		ResultSet result = statement.executeQuery("SELECT email, name, surname, address FROM `users` WHERE id=\""+ id + "\"");
    		if(result.next()){
    			if(!(name.equals("") || surname.equals("") || email.equals("") || address.equals(""))){
    				if(Tool.checkEmailFormat(email)){
    					if(Tool.checkPeselFormat(pesel)){
    						statement.executeUpdate("Update users set name=\""+name+"\", surname=\""+surname+"\", address=\""+address+"\", email=\""+email+"\" WHERE id='"+id+"'");
							result.close();
							connection.close();
							target="success";
							actionErrors.add("info",
									new ActionMessage("error.userChanged"));
    					}else{
    					//bledny format email
    						target="failure";
        					result.close();
        					connection.close();
        					actionErrors.add("info",
    								new ActionMessage("error.pesel"));
    					}
    				}else{
    					//zly pesel
    					
    					actionErrors.add("info",
								new ActionMessage("error.emailFormat"));
						target="failure";
						result.close();
						connection.close();
    				}
    			}else{
    				//puste pola
    				actionErrors.add("info",
							new ActionMessage("error.emptyFields"));
    				target="failure";
    				result.close();
					connection.close();
    			}
    		}else{
    			target="failure";
    			//blad resultset
    			actionErrors.add("info",
						new ActionMessage("error.resultSet"));
    			result.close();
				connection.close();
    		}
		}catch(Exception e){
			target="failure";
			actionErrors.add("info",
					new ActionMessage("error.catch"));
		}
		if(actionErrors.size() !=0){
			extracted(request, actionErrors);
			return new ActionForward(mapping.getInput());
		}
		
		return mapping.findForward(target);
	}
	
	private void extracted(HttpServletRequest request, ActionErrors actionErrors) {
		saveErrors(request, actionErrors);
	}
	


	

}
